package com.zhiyou.app.shop.manager.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.zhiyou.app.shop.common.Authority;
import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.InnerObject;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.service.IShopManageService;

public class ShopAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IShopManageService shopManageService;

	private List<Shop> listShop;
	
	private Shop shop = new Shop();
	
	private String ids;
	
	public ShopAction() {
		super();
		this.setCurrentPath(getText("shopManage.path.myshop.shop"));
	}
	
	/**
	 * 点击商铺管理中的“我的商铺”调用此方法
	 * @return
	 */
	@Authority(2)
	public String myShop(){
		try {
			this.shop = shopManageService.findMyShop(this.getCurrentUser().getId());
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	/**
	 * 开通商铺或编辑商铺信息
	 * @return
	 */
	@Authority(2)
	public String saveOrUpdateMyShop(){
		try {
			Validator.validateShopAdd(shop);
			boolean isCreate = StringUtils.isBlank(shop.getId()) ? true : false;
			shopManageService.saveOrUpdateMyShop(shop);
			if(isCreate){
				this.setMessage("shopManage.remind.createShop.success");
			}else{
				this.setMessage("shopManage.remind.editShop.success");
			}
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 开放或者关闭商铺
	 * @return
	 */
	@Authority(1)
	public String changeOpenState(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				shopManageService.closeOrOpenShop(ids, shop.getIsDelete());
			}
		});
		return null;
	}
	
	/**
	 * 开放或者关闭商铺
	 * @return
	 */
	@Authority(1)
	public String updateShopOrderNumber(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				int orderNumber = shop.getOrderNumber();
				shop = shopManageService.findShopById(shop.getId());
				shop.setOrderNumber(orderNumber);
				shopManageService.updateShop(shop);
			}
		});
		return null;
	}
	
	private Integer shopState;
	/**
	 * 公司管理员管理商铺列表页
	 * @return
	 */
	@Authority(1)
	public String shopList(){
		if(null != shopState){
			shop.setIsDelete(shopState == 0 ? false : true);
		}
		try {
			shopManageService.getShopBypage(this.getInitedPage(), shop);
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	
	public List<Shop> getListShop() {
		return listShop;
	}

	public void setListShop(List<Shop> listShop) {
		this.listShop = listShop;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Integer getShopState() {
		return shopState;
	}

	public void setShopState(Integer shopState) {
		this.shopState = shopState;
	}

}
